﻿
Public Module parseerror
    Public Const logerrorcount As Int64 = 4

    Private Class errordef
        Implements IComparable(Of errordef)
        Public msg As String = Nothing
        Public point As Int64 = 0

        Public Sub New(ByVal msg As String, ByVal point As Int64)
            copy(Me.msg, msg)
            copy(Me.point, point)
        End Sub

        Public Function CompareTo(ByVal other As errordef) As Int32 Implements IComparable(Of errordef).CompareTo
            If Me Is Nothing AndAlso other Is Nothing Then
                Return 0
            ElseIf Me Is Nothing Then
                Return -1
            ElseIf other Is Nothing Then
                Return 1
            Else
                Dim cmp As Int64
                cmp = compare(point, other.point)
                If cmp = 0 Then
                    Return compare(msg, other.msg)
                Else
                    Return cmp
                End If
            End If
        End Function
    End Class

    Private Class errors
#If Not PocketPC AndAlso Not Smartphone Then
        <ThreadStatic()> Public Shared e(logerrorcount - 1) As errordef
#Else
        Public Shared e(logerrorcount - 1) As errordef
#End If
    End Class

    Public Sub logparseerror(ByVal msg As String, ByVal point As Int64)
        Dim newerr As errordef = Nothing
        newerr = New errordef(msg, point)
        If errors.e Is Nothing Then
            ReDim errors.e(logerrorcount - 1)
        End If
        Dim i As Int64
        For i = 0 To logerrorcount - 1
            If compare(errors.e(i), newerr) < 0 Then
                errors.e(i) = newerr
                Exit For
            End If
        Next

        sort(errors.e)
    End Sub

    Public Delegate Sub raiseErrorDelegate(ByVal errmsg As String, _
                                           ByVal errType As errorHandle.errorType)

    Private raiseError As raiseErrorDelegate = AddressOf Global.raiseError

    Public Sub setraiseError(ByVal re As raiseErrorDelegate)
        raiseError = re
    End Sub

    Public Function getraiseError() As raiseErrorDelegate
        Return raiseError
    End Function

    Public Sub writeparseerror(ByVal src As String)
        If Not errors.e Is Nothing Then
            If raiseError Is Nothing Then
                raiseError = AddressOf Global.raiseError
            End If
            Dim i As Int64
            For i = logerrorcount - 1 To 0 Step -1
                If errors.e(i) Is Nothing Then
                    Exit For
                Else
                    raiseError(errors.e(i).msg + ", " + displaySource(src, errors.e(i).point), _
                               errorHandle.errorType.user)
                End If
            Next
        End If
    End Sub
End Module
